<?php

if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
$objXajax->registerFunction('fmfrmmonitoring_init');
$objXajax->registerFunction('fmfrmmonitoring_loadGrid');
$objXajax->registerFunction('fmfrmmonitoring_loadProduct');


/*
* ham dung cho left menu
* 
*/
//global $fmpromotype;

function fmfrmmonitoring_init(){
    global $objDbSelect,$lang,$arrMenu,$arrdetail,$hotel_id;
    $objResponse = new xajaxResponse();
   
    $objResponse->addScript("xajax_fmfrmmonitoring_loadGrid();");                       
                           
    return $objResponse->getXML();
}

/*function fmfrmmonitoring_loadMenu(){     

    global $lang,$arrMenu,$arrdetail;
    $objResponse = new xajaxResponse();
    $arr = array();
    $arrdetail= array();
    $arrMain = common_getMenu($arr,$arrdetail);
    $oTbs = new clsTinyButStrong();
    $oTbs ->LoadTemplate("templates/$lang/customer/fm_manager_menu_grid.html");
    $oTbs->MergeBlock('blk_list', $arrMain);
    $oTbs->MergeBlock('blk_list1', 'array', 'arrdetail[%p1%]');
        
    $objResponse->addAssign('fmfrmmonitoring_leftmenu', 'innerHTML', $oTbs->Source);
     
    $count = count($arr);
    for($i=0;$i<$count;$i++){ // 
        $name     = $arr[$i]['name'];
        $price    = $arr[$i]['price'];
        $id       = $arr[$i]['id'];
        $unit     = $arr[$i]['unit'];
        $unitname = $arr[$i]['unitname'];
        $area_ext = $arr[$i]['area_ext'];
        $objResponse->addScript("fm_obj.createAssignProductPrice($id,'$name','$price','$unit','$unitname',$area_ext);"); 
    }
    $indexNewProduct = time();
    $objResponse->addScript("fm_obj.indexNewProduct=$indexNewProduct;");
    $objResponse->addScript("fmfrmmonitoring_dropdrapmenu();");  
    $objResponse->addScript("changeWaiting(1);");  
                          
    return $objResponse->getXML();
}*/

/*function fmfrmmonitoring_loadRoom(){ 
    global $lang,$arrDetail,$objDbSelect;
        $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
        $arrDetail = fmfrmmonitoring_getRoomInit();
         
        for($i=0;$i<count($arrDetail);$i++){
            $id = $arrDetail[$i]['id'];
            $name = $arrDetail[$i]['name'];
            $type = $arrDetail[$i]['type_id'];            
            $extprice= $arrDetail[$i]['extprice'];
            $objResponse->addScript("fm_obj.createAssignRoomObj($id,'$name','$type','$extprice'"); 
        }

    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}*/

function fmfrmmonitoring_loadGrid($col=0){     

    global $lang,$arrMenu,$arrDetail,$hotel_id,$objDbSelect;
    $objResponse = new xajaxResponse();
    $arrDetail = array();
    $arrRoom = fmfrmmonitoring_getRoom($arrDetail,$arr);
        
/*     for($i=0;$i<count($arr);$i++){
            $id = $arr[$i]['id'];
            $name = $arr[$i]['name'];
            $type = $arr[$i]['type_id'];            
            $extprice= $arr[$i]['extprice'];
            $objResponse->addScript("fm_obj.createAssignRoomObj($id,'$name','$type','$extprice')"); 
       }  */
        
        

    $oTbs = new clsTinyButStrong();    
    $oTbs->LoadTemplate('templates/'.$lang."/customer/fm_monitoring_grid.html");   
    $oTbs->MergeBlock('blk_total', $arrRoom); 
    
    $oTbs->MergeBlock('blk_data', 'array', 'arrDetail[%p1%]'); 
    

    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('fmfrmmonitoring_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScript("setTimeout('fmfrmmonitoring_firstload()',2000);"); 
/*    $objResponse->addScript("addJsContext();");  
     
   $objResponse->addScript('$(".clsbtn_fm").button();');          
   $objResponse->addScript('$(".fm_chkHide").button();');          
   $objResponse->addScript('xajax_fmfrmmonitoring_loadMenu();'); 
             
   $objResponse->addScriptCall("fmfrmmonitoring_checkundobtn();");*/
             
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function  fmfrmmonitoring_getRoom(&$arrDetail,&$arr){
    global $objDbSelect,$tbl_postfix,$hotel_id;      
    $arrResult = array();
    $sqlSelect = "SELECT r.id,r.room_type,r.name,t.style as color,t.name  as name_type,t.id as type_id ,t.extprice
    FROM fm_room as r join fm_room_type  as t on r.room_type=t.id and t.service_id = $hotel_id where r.active = 1 and t.active = 1
     order by r.room_type,r.name";
    $arr =  $objDbSelect->GetArray($sqlSelect);
    for($i=0;$i<count($arr);$i++){
        $type_id = $arr[$i]['type_id'];
        $arrResult[$type_id]= array('type_id'=>$arr[$i]['type_id'],'name_type'=>$arr[$i]['name_type']);
        $arrDetail[$type_id][]=array('id'=>$arr[$i]['id'],'color'=>$arr[$i]['color'],'name'=>$arr[$i]['name']);
    }
    return $arrResult;
}
/*
* 
*/
function  fmfrmmonitoring_getRoomInit(){
    global $objDbSelect,$tbl_postfix,$hotel_id;
    $arrResult = array();
    $sqlSelect = "SELECT r.id,r.name as name,r.room_type,t.extprice,t.style ,t.name  as name_type,t.id as type_id
    FROM fm_room as r join fm_room_type  as t on r.room_type=t.id  and t.service_id = $hotel_id join cus_config cf on 
    cf.service_id = $hotel_id where r.active = 1  order by r.room_type,r.name";
    $arr =  $objDbSelect->GetArray($sqlSelect);
    return $arr;
}   

/*
* HÃ m nÃ y dÃ¹ng Ä‘á»ƒ load product lÃªn cÃ¡c bÃ n 
* 
* 
*/
function fmfrmmonitoring_loadProduct($table_id=''){
    global $objDbSelect,$tbl_postfix,$hotel_id;
     $objResponse = new xajaxResponse();
     //$service_type =  SVT_FILM;
     $condition =  $table_id==''?'':" and table_id= '$table_id' " ;
    $sql = "select p.name,(p.price+p.area_extprice) as price,sum(p.num) as num,p.product_id,p.table_id as room_id,p.type_pro  from cus_history_product$tbl_postfix as p 
where p.num>0 and p.flg = 0 $condition group by p.product_id,table_id,area_extprice order by p.table_id, p.type_pro, p.date_created"; // lấy sản phẩm trên bàn từ DB
    $arr = $objDbSelect->GetArray($sql);
    //echo $sql; die();
    for($i=0;$i<count($arr);$i++){
        $room_id = $arr[$i]['room_id'];
        $name = addslashes($arr[$i]['name']);
        $num = $arr[$i]['num'];
        $price = $arr[$i]['price'];
        $product_id = $arr[$i]['product_id'];
        $type_pro = $arr[$i]['type_pro'];
        $objResponse->addScript("fmfrmmonitoring_loadProduct('$room_id','$product_id','$name','$num','$price','$type_pro');"); 
    }
    $sql_total = " SELECT SUM(c.num*(c.price+c.area_extprice)) AS total FROM cus_history_product$tbl_postfix AS c WHERE 
c.flg=1  AND DATEDIFF(c.date_update,NOW())=0 ";
//echo $sql_total; die();
    $arrTotal = $objDbSelect->GetArray($sql_total);
    $total = $arrTotal[0]['total'];
    $money_get = 0;
        $money_pay =0;
        $sqlpay = "select sum(money) as money,flg from cus_pay where datediff(date_created,now())=0 and service_id = '$hotel_id' group by flg ";
        $arrPay = $objDbSelect->GetArray($sqlpay);
        for($i=0;$i<count($arrPay);$i++){
            if($arrPay[$i]['flg']==1){
               $money_get = $arrPay[$i]['money']; 
            }else{
                $money_pay = $arrPay[$i]['money']; 
            }
        }
        $money_revenue = $total - $money_pay + $money_get;
        $total = FormatMoney($total);
        $money_revenue = FormatMoney($money_revenue);
        $money_pay = FormatMoney($money_pay);
        $money_get = FormatMoney($money_get);

        $objResponse->addAssign('fmfrmmonitoring_doanhthu', 'innerHTML', $total);
        $objResponse->addScript("$('#fmfrmmonitoring_moneyget').html('$money_get'); "); 
        $objResponse->addScript("$('#fmfrmmonitoring_moneypay').html('$money_pay'); "); 
        $objResponse->addScript("$('#fmfrmmonitoring_revenue').html('$money_revenue'); ");
        
     $objResponse->addScript("fm_addJsContext();"); 
     $objResponse->addScript("changeWaiting(1);"); 
   return $objResponse->getXML();  
}


    
?>